/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package viewControl;

import entity.*;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import util.GUIUtils;

/**
 *
 * @author menderleit
 */
public class PotentialPanel extends javax.swing.JPanel {

    private Session session;
    private Laerer currentLaerer;

    /**
     * Creates new form PotentialPanel
     */
    public PotentialPanel(Session session) {
        this.currentLaerer = null;
        initComponents();
        this.session = session;
    }

    public PotentialPanel(Session session, Laerer laerer) {
        initComponents();
        this.session = session;
        this.currentLaerer = laerer;
    }
    
    protected void update() {
        // Udfyld lærer combobox.
        cbLaererNavn.removeAllItems();

        if (currentLaerer == null) {
            String hql;
            cbLaererNavn.addItem("Lærer");
            hql = "from Laerer";
            try {
                session.beginTransaction();
                Query q = session.createQuery(hql);
                List resultList = q.list();
                for (int i = 0; i < resultList.size(); i++) {
                    cbLaererNavn.addItem((Laerer) resultList.get(i));
                }
                session.getTransaction().commit();
                session.flush();
            } catch (HibernateException he) {
                System.out.println(he.getLocalizedMessage());
            }
        } else if (currentLaerer != null) {
            cbLaererNavn.addItem(currentLaerer);
            cbLaererNavn.setEnabled(false);
            validate();
        }
        GUIUtils.clearFields(this);
        this.revalidate();
        this.repaint();
    }

    protected void updateStudents() {
        if (cbLaererNavn.getSelectedIndex() > 0 || currentLaerer != null) {
            if (currentLaerer == null) {
                currentLaerer = (Laerer) cbLaererNavn.getSelectedItem();
            }
            cbElevNavn.removeAllItems();
            cbElevNavn.addItem("Elev");
            ArrayList holdfags = new ArrayList();
            ArrayList elever = new ArrayList();
            ArrayList finallist = new ArrayList();
            holdfags.addAll(currentLaerer.getHoldfags());
            for (int i = 0; i < holdfags.size(); i++) {
                Holdfag hf = (Holdfag) holdfags.get(i);
                elever.addAll(hf.getHold().getElevs());
                for (int j = 0; j < elever.size(); j++) {
                    Elev e = (Elev) elever.get(j);
                    if (!finallist.contains(e)) {
                        finallist.add(e);
                        cbElevNavn.addItem(e);
                    }
                }
            }
            cbElevNavn.setEnabled(true);
        } else {
            cbElevNavn.setEnabled(false);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbElevNavn = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        listPotentiale = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        listKompetence = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cbLaererNavn = new javax.swing.JComboBox();
        btnTilPotentiale = new javax.swing.JButton();
        btnTilKompetence = new javax.swing.JButton();
        btnSletPotentiale = new javax.swing.JButton();
        btnSletKompetence = new javax.swing.JButton();
        cbPotentialeFag = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();

        setLayout(null);

        cbElevNavn.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Elev" }));
        cbElevNavn.setEnabled(false);
        cbElevNavn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbElevNavnActionPerformed(evt);
            }
        });
        add(cbElevNavn);
        cbElevNavn.setBounds(10, 40, 209, 20);

        jScrollPane1.setViewportView(listPotentiale);

        add(jScrollPane1);
        jScrollPane1.setBounds(10, 130, 610, 200);

        jScrollPane2.setViewportView(listKompetence);

        add(jScrollPane2);
        jScrollPane2.setBounds(10, 420, 610, 200);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Potentialer");
        add(jLabel1);
        jLabel1.setBounds(10, 110, 63, 14);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Kompetencer");
        add(jLabel2);
        jLabel2.setBounds(10, 400, 76, 14);

        cbLaererNavn.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Lærer" }));
        cbLaererNavn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbLaererNavnActionPerformed(evt);
            }
        });
        add(cbLaererNavn);
        cbLaererNavn.setBounds(10, 11, 209, 20);

        btnTilPotentiale.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnTilPotentiale.setText("Potentiale >> Kompetence");
        btnTilPotentiale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTilPotentialeActionPerformed(evt);
            }
        });
        add(btnTilPotentiale);
        btnTilPotentiale.setBounds(10, 340, 210, 23);

        btnTilKompetence.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnTilKompetence.setText("Kompetence << Potentiale");
        btnTilKompetence.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTilKompetenceActionPerformed(evt);
            }
        });
        add(btnTilKompetence);
        btnTilKompetence.setBounds(10, 630, 210, 23);

        btnSletPotentiale.setText("Slet Potentiale");
        btnSletPotentiale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSletPotentialeActionPerformed(evt);
            }
        });
        add(btnSletPotentiale);
        btnSletPotentiale.setBounds(410, 340, 210, 23);

        btnSletKompetence.setText("Slet Kompetence");
        btnSletKompetence.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSletKompetenceActionPerformed(evt);
            }
        });
        add(btnSletKompetence);
        btnSletKompetence.setBounds(410, 630, 210, 23);

        cbPotentialeFag.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Fag" }));
        cbPotentialeFag.setEnabled(false);
        cbPotentialeFag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbPotentialeFagActionPerformed(evt);
            }
        });
        add(cbPotentialeFag);
        cbPotentialeFag.setBounds(10, 70, 210, 20);
        add(jSeparator1);
        jSeparator1.setBounds(0, 380, 1240, 10);
    }// </editor-fold>//GEN-END:initComponents

    private void cbElevNavnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbElevNavnActionPerformed
        ArrayList fag = new ArrayList();
        if (cbElevNavn.getSelectedIndex() > 0) {
            cbPotentialeFag.setEnabled(true);
            Elev e = (Elev) cbElevNavn.getSelectedItem();
            ArrayList holdfaglist = new ArrayList();
            ArrayList laererfaglist = new ArrayList();
            holdfaglist.addAll(e.getHold().getHoldfags());
            laererfaglist.addAll(currentLaerer.getHoldfags());
            for (int i = 0; i < holdfaglist.size(); i++) {
                Holdfag elevfag = ((Holdfag) holdfaglist.get(i));
                for (int j = 0; j < laererfaglist.size(); j++) {
                    Holdfag laererfag = ((Holdfag) laererfaglist.get(j));
                    if (laererfag.equals(elevfag)) {                      
                        fag.add(elevfag.getFag());
                    }
                }
            }
            cbPotentialeFag.removeAllItems();
            cbPotentialeFag.addItem("Fag");
            for (int i = 0; i < fag.size(); i++) {
                cbPotentialeFag.addItem(fag.get(i));
            }
        } else {
            cbPotentialeFag.setSelectedIndex(0);
            cbPotentialeFag.setEnabled(false);
        }
    }//GEN-LAST:event_cbElevNavnActionPerformed

    private void btnTilPotentialeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTilPotentialeActionPerformed
        Potentiale p = (Potentiale) listPotentiale.getSelectedValue();
        if (p != null) {
            p.setKompetence(true);
            try {
                session.beginTransaction();
                session.update(p);
                session.getTransaction().commit();
            } catch (HibernateException ex) {
                System.out.println("cbElevNavnActionPerformed: " + ex.getLocalizedMessage());
            }
            cbPotentialeFagActionPerformed(evt);
        }
    }//GEN-LAST:event_btnTilPotentialeActionPerformed

    private void btnTilKompetenceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTilKompetenceActionPerformed
        Potentiale p = (Potentiale) listKompetence.getSelectedValue();
        if (p != null) {
            p.setKompetence(false);
            try {
                session.beginTransaction();
                session.update(p);
                session.getTransaction().commit();
            } catch (HibernateException ex) {
                System.out.println("cbElevNavnActionPerformed: " + ex.getLocalizedMessage());
            }
            cbPotentialeFagActionPerformed(evt);
        }
    }//GEN-LAST:event_btnTilKompetenceActionPerformed

    private void btnSletPotentialeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSletPotentialeActionPerformed
        Potentiale p = (Potentiale) listPotentiale.getSelectedValue();
        if (p != null) {
            try {
                p.getElev().getPotentiales().remove(p);
                session.beginTransaction();
                session.delete(p);
                session.getTransaction().commit();
            } catch (HibernateException ex) {
                System.out.println("cbElevNavnActionPerformed: " + ex.getLocalizedMessage());
            }
            cbPotentialeFagActionPerformed(evt);
        }
    }//GEN-LAST:event_btnSletPotentialeActionPerformed

    private void btnSletKompetenceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSletKompetenceActionPerformed
        Potentiale p = (Potentiale) listKompetence.getSelectedValue();
        if (p != null) {
            try {
                p.getElev().getPotentiales().remove(p);
                session.beginTransaction();
                session.delete(p);
                session.getTransaction().commit();
            } catch (HibernateException ex) {
                System.out.println("cbElevNavnActionPerformed: " + ex.getLocalizedMessage());
            }
            cbPotentialeFagActionPerformed(evt);
        }
    }//GEN-LAST:event_btnSletKompetenceActionPerformed

    private void cbLaererNavnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbLaererNavnActionPerformed
        updateStudents();
        if (cbLaererNavn.getSelectedIndex() == 0 && currentLaerer == null) {
            cbElevNavn.setEnabled(false);
            cbPotentialeFag.setEnabled(false);
        }
    }//GEN-LAST:event_cbLaererNavnActionPerformed

    private void cbPotentialeFagActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbPotentialeFagActionPerformed

        ArrayList<Potentiale> pt = new ArrayList();
        ArrayList<Potentiale> kp = new ArrayList();
        if (cbElevNavn.getSelectedIndex() > 0 && cbPotentialeFag.getSelectedIndex() > 0) {
            listPotentiale.setListData(new Object[0]);
            Fag f = (Fag) cbPotentialeFag.getSelectedItem();
            String hql = "from Potentiale where cpr=" + ((Elev) cbElevNavn.getSelectedItem()).getCpr();
            try {
                session.beginTransaction();
                Query q = session.createQuery(hql);
                Object[] hs = q.list().toArray();

                for (int i = 0; i < hs.length; i++) {
                    if (((Potentiale) hs[i]).getTrinbeskrivelse().getTrinoverskrift().getFag().equals(f)) {
                        if (((Potentiale) hs[i]).getKompetence()) {
                            kp.add((Potentiale) hs[i]);
                        } else {
                            pt.add((Potentiale) hs[i]);
                        }
                    }
                }
                listPotentiale.setListData(pt.toArray());
                listKompetence.setListData(kp.toArray());
                session.getTransaction().commit();
                session.flush();
            } catch (HibernateException ex) {
                System.out.println("cbElevNavnActionPerformed: " + ex.getLocalizedMessage());
            }
        } else {
            listPotentiale.setListData(pt.toArray());
            listKompetence.setListData(kp.toArray());
        }

    }//GEN-LAST:event_cbPotentialeFagActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSletKompetence;
    private javax.swing.JButton btnSletPotentiale;
    private javax.swing.JButton btnTilKompetence;
    private javax.swing.JButton btnTilPotentiale;
    private javax.swing.JComboBox cbElevNavn;
    private javax.swing.JComboBox cbLaererNavn;
    private javax.swing.JComboBox cbPotentialeFag;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JList listKompetence;
    private javax.swing.JList listPotentiale;
    // End of variables declaration//GEN-END:variables
}
